var appPath = getAppPath();	

$(function(){
	
	// 大分类
	$("#queryTmplType").combobox({
		url:appPath+'/itbTg/getTypeForCombo.action?topIds=1,2,3,4,6,7,8,1010,1011,1030&blkFlg=1',
		valueField:'type_id',
		textField:'type_name',
		onSelect:function(selRecd){
			$("#queryTmplType2").combobox("clear");
			$("#queryTmplType3").combobox("clear");
			$("#queryTmplType2").combobox("loadData",{});
			$("#queryTmplType3").combobox("loadData",{});
			
			// 中分类
			var parentId=selRecd.type_id;
			if(Number(parentId)>0){
				$("#queryTmplType2").combobox({
					url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId+'&blkFlg=1',
					valueField:'type_id',
					textField:'type_name',
					onSelect:function(selRecd2){
						$("#queryTmplType3").combobox("clear");
						$("#queryTmplType3").combobox("loadData",{});
						
						// 小分类
						var parentId2=selRecd2.type_id;
						if(Number(parentId2)>0){
							$("#queryTmplType3").combobox({
								url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId2+'&blkFlg=1',
								valueField:'type_id',
								textField:'type_name',
								onSelect:function(selRecd3){
									// TODO
								}
							});
						}
					}
				});
			}
		}
	});
	
	initJspGrid();
});

// 样版列表初始化
function initJspGrid(){
	
	var toolBar = [];
	if(1==Number(g_bAdmin)) {
		toolBar = [{
			text:'发布分页映射',
			iconCls:'icon-ok',
			handler:function(){
				doPublish();
			}
		},
		{
			text:'取消发布',
			iconCls:'icon-cancel',
			handler:function(){
				cancelPublish();
			}
		},
		{
			text:'删除分页映射',
			iconCls:'icon-cancel',
			handler:function(){
				delTmpl();
			}
		}];
	}
	
	$('#tmplGrid').datagrid({
	    url:appPath + "/itbMap/getMappingInfoList.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
			{field:'editTmpl',title:'分页预览',width:40,align:'center',sortable:false,
				formatter:function(value,rowData,rowIndex){
					if(!rowData.map_id) return "";
					var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='分页预览' ";
					ret += " onClick='editTmpl("+rowData.map_id+","+rowData.publish+")'/></a>";
					return ret;
				}
			},
	        {field:'map_id',title:'文档ID',width:40,align:'left',sortable:false},
	        {field:'tg_id',title:'提纲ID',width:40,align:'left',sortable:false,
		    	  formatter:function(tgId, rowData, rowIndex){
		    		  return '<span style="color:red">'+tgId+'</span>';
		    	  }
		      },
	        {field:'size',title:'尺寸',width:60,align:'left',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return 'W('+(rowData.width||'N/A')+')*H('+(rowData.height||'N/A')+')';
		    	  }
		      },
		    {field:'top_type',title:'主分类',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
//		    		  var tn = '<span style="color:red">未知</span>';
//		    		  switch(Number(value||'0')){
//		    		  	case 1: tn='单页';break;
//		    		  	case 2: tn='多页';break;
//		    		  	case 3: tn='PPT';break;
//		    		  	case 4: tn='印品';break;
//		    		  	case 6: tn='社交媒体';break;
//		    		  	case 7: tn='电商';break;
//		    		  	case 8: tn='微商';break;
//		    		  }
		    		  return rowData.top_type_name || '<span style="color:red">未知</span>';
		    	  }
		      },
	        {field:'editType',title:'分类',width:70,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return rowData.type_name || '<span style="color:red">未分类</span>';
		    	  }
		      },
	        {field:'order',title:'显示顺序',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.tg_id) return "";
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.map_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		      },
	        {field:'map_name',title:'分页映射名称',width:100,align:'left',sortable:false},
	        {field:'publish',title:'制作状态',width:45,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '已完成';
	        		return '制作中';
	        	}}, 
        	{field:'make',title:'设计模版',width:45,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		if (Number(rowData.publish) == 1) {
	        			return "<a href='javascript:void(0)' onClick='makeDesign("+rowData.map_id+")'>"
	    		  			+'<span style="color:green">创建</span>';
	        		} else {
	        			return '<span style="color:red">N/A</span>';
	        		}
	        	}},
	        {field:'create_time',title:'创建时间',width:70,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:70,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar: toolBar
	    
	});

}


/**
 * 编辑分页映射
 */
function editTmpl(mapId, publish){
	//if (publish == 1 || publish =="1") {
	//	$.messager.alert('提示信息','请勿编辑已发布分页映射！','warning');
	//} else {
		var editorUrl = appPath+"/js/mind/idxMap.jsp?id="+mapId;
		window.open(editorUrl);
	//}
}

/**
 * 删除分页映射
 */
function delTmpl(){
	var selectItem=$("#tmplGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择分页映射','warning');
		return;
	}
	var bOK = true;
	var ids = "";
	$.each(selectItem, function(index, item){
		if(Number(item.publish||'0')==1) {
			bOK = false;
			return false;
		}
		ids = ids + item.map_id + ",";
	});
	if(!bOK) {
		$.messager.alert('提示信息','请勿删除已发布分页映射','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认删除", function (r) {  
        if (r) {
			var url = appPath+"/itbMapWeb/deleteMapping.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','删除成功','info');
					doQuery();
				}else{
					$.messager.alert('提示信息','删除失败','info');
				}
			});
        }
	});
}

/**
 * 显示顺序
 */
function editOrder(mapId, order){
	$("#mapId").val(mapId);
	$("#order").val(order);
	
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}
/**
 * 修改显示顺序
 */
function doUpdOrder(){
	var mapId = $("#mapId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	
	var url = appPath+"/itbMapWeb/updMappingOrder.action?mapId="+mapId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}
/**
 * 结束显示顺序编辑
 */
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}

/**
 * 发布
 */
function doPublish(){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length == 0){
		$.messager.alert('提示信息','请选择未发布分页映射进行处理！','warning');
		return;
	}else if(selectTmpl.length>1){
		$.messager.alert('提示信息','请选择单个分页映射进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(item.publish != 1 && item.publish != '1'){
			doFlg = true;
			ids = ids + item.map_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择未发布提纲实例进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认",
		"是否确认发布分页映射？<br/><span style='color:red'>#请确保已经过认真检查</span>",
		function (r) {
	        if (r) {
	        	var url = appPath+"/itbMapWeb/doPublish.action?ids="+ids;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.messager.alert('提示信息','分页映射发布成功！','info');
	        			doQuery();
	        		} else if (data=="ng"){
	        			// TODO
	        			$.messager.alert('提示信息','ng！','warning');
	        		}
	        	});
	        }
		}
	);
}
/**
 * 取消发布
 */
function cancelPublish(){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length == 0){
		$.messager.alert('提示信息','请选择已发布分页映射进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(item.publish == 1 || item.publish == '1'){
			doFlg = true;
			ids = ids + item.map_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择已发布分页映射进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认取消已发布分页映射？", function (r) {
		if(r){
			var url = appPath+"/itbMapWeb/cancelPublish.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','分页映射已取消发布！','info');
					doQuery();
				}
			});
		}
	});
}

/**
 * 创建设计模版
 */
function makeDesign(mapId){
	// 非加密方式
	// var editorUrl = getDesignServer()+"/design/getWorkEditors?"+"uId="+g_userId+"&mId="+mapId;
	// 加密方式
	var editorUrl = getDesignServer()+'/encrypt/encrypt'+urlWithPara({uId:g_userId,mId:mapId,destination:'/design/getWorkEditors'});
	
	window.open(editorUrl);
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	
	var findTmplType =$("#queryTmplType3").combobox('getValue');
	if(!findTmplType){
		findTmplType =$("#queryTmplType2").combobox('getValue');
		if(!findTmplType){
			findTmplType=$("#queryTmplType").combobox('getValue');
		}
	}
					
	var findPubFlg = $("#queryPublish").combobox('getValue');
	var findTmplName=$("#queryTmplName").val();
	
	if (findTmplType) {
		queryObject.findTmplType = findTmplType;// 类型
	}
	if(findPubFlg&&findPubFlg.length>0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 样版名称
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(startFlg){
	$("#tmplGrid").datagrid('unselectAll');
	if(startFlg==1) {
		$("#tmplGrid").datagrid('load', getQueryParam());
	} else {
		$("#tmplGrid").datagrid('reload', getQueryParam());
	}
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
